package jin.si.controller;

import jin.si.dao.ShoppingMapper;
import jin.si.pojo.JsRegion;
import jin.si.pojo.JsUserAddress;
import jin.si.pojo.JsUserCoupon;
import jin.si.pojo.JsUserOrderShop;
import jin.si.service.ShoppingService;
import jin.si.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * ClassName:ShoppingController
 * Package:jin.si.controller
 * Description:
 *
 * @Date:2022/4/8 14:15
 * @Author: XJR
 */
@RequestMapping("/shopping")
@RestController
public class ShoppingController {
    @Autowired
    private ShoppingService service;

    //加入购物车
    @RequestMapping("/addShopOrder")
    public JsonData addShopOrder(JsUserOrderShop jsUserOrderShop){
        Boolean addShopOrder = service.addShopOrder(jsUserOrderShop);
        return addShopOrder!=false? JsonData.buildSuccess(addShopOrder):JsonData.buildError("服务器发生错误");
    };

    //查看是否有默认地址
    @RequestMapping("/selectUserDefaultCity")
    public JsonData selectUserDefaultCity(Integer userId,Integer isDefault){
        List<JsUserAddress> jsUserAddress = service.selectUserDefaultCity(userId, isDefault);
        return jsUserAddress!=null?JsonData.buildSuccess(jsUserAddress):JsonData.buildError("服务器发生错误");
    };

    //查询可选择的地址
    @RequestMapping("/selectJsRegionAddress")
    public JsonData selectJsRegion(Integer parentCode){
        List<JsRegion> jsRegions = service.selectJsRegion(parentCode);
        return jsRegions!=null?JsonData.buildSuccess(jsRegions):JsonData.buildError("服务器发生错误");
    };

    //查询可用以及不可用优惠券
    @RequestMapping("/selectJsCouponYesOrNo")
    public JsonData selectJsCouponYesOrNo(JsUserCoupon jsUserCoupon){
        List<JsUserCoupon> jsUserCoupons = service.selectJsCouponYesOrNo(jsUserCoupon);
        return jsUserCoupons!=null?JsonData.buildSuccess(jsUserCoupons):JsonData.buildError("服务器发生错误");
    };

    //添加用户的收货地址
    @RequestMapping("/addUserAddress")
    public JsonData addUserAddress(JsUserAddress jsUserAddress){
        Boolean addUserAddress = service.addUserAddress(jsUserAddress);
        return addUserAddress!=false?JsonData.buildSuccess(addUserAddress):JsonData.buildError("服务器发生错误");
    };

    //查询指定用户的所有地址（默认和非默认）
    @RequestMapping("/selectJsAddressAll")
    public JsonData selectJsAddressAll(Integer userId){
        List<JsUserAddress> jsUserAddresses = service.selectJsAddressAll(userId);
        return jsUserAddresses!=null?JsonData.buildSuccess(jsUserAddresses):JsonData.buildError("服务器发生错误");
    };

    //将当前用户所有的地址都改成非默认
    @RequestMapping("/updateUserNoDefault")
    public JsonData updateUserNoDefault(Integer userId,Integer isDefaultt){
        Boolean updateUserNoDefault = service.updateUserNoDefault(userId, isDefaultt);
        return updateUserNoDefault!=false?JsonData.buildSuccess(updateUserNoDefault):JsonData.buildError("服务器发生错误");
    };

    //修改用户的默认地址
    @RequestMapping("/updateUserIsDefault")
    public JsonData updateUserIsDefault(Integer isDefault,Integer addressId,Integer userId,Integer isDefaultt){
        Boolean updateUserNoDefault = service.updateUserNoDefault(userId, isDefaultt);
        Boolean updateUserIsDefault = service.updateUserIsDefault(isDefault, addressId);
        return updateUserNoDefault&&updateUserIsDefault!=false?JsonData.buildSuccess(updateUserIsDefault):JsonData.buildError("服务器发生错误");
    };
}
